package com.bdqn.controller;

import com.bdqn.entity.provider;
import com.bdqn.entity.user;
import com.bdqn.service.smbms_providerService;
import com.bdqn.util.page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/provider")
public class providerController {

    @Autowired
    private smbms_providerService prodiver;

    @RequestMapping("/getAll")
    public String findAllList(@RequestParam(required = false) String queryProCode,
                              @RequestParam(required = false) String queryProName,
                              @RequestParam(required = false) Integer currentPageNo,
                              Model model, HttpServletRequest request){
        if (currentPageNo == null){
            currentPageNo = 1;
        }
        Map map = new HashMap();
        map.put("proName",queryProName);
        map.put("proCode",queryProCode);
        map.put("currentPageNo",(currentPageNo-1)*10);
        map.put("pageSize",10);

        page pa = new page();
        pa.setPageSize(10);
        pa.setTotalCount(prodiver.findAllCount(map));
        pa.setCurrentPageNo(currentPageNo);
        List<provider> providerList = prodiver.findproviderByparams(map);
        request.getSession().setAttribute("providerList",providerList);
        request.getSession().setAttribute("page",pa);
        request.getSession().setAttribute("queryProCode",queryProCode);
        request.getSession().setAttribute("queryProname",queryProName);
        return "jsp/providerlist";
    }


    @RequestMapping("/{id}")
    public String getUser(@PathVariable(value = "id") Integer id, HttpServletRequest request, String opr){
        request.getSession().setAttribute("provider",prodiver.getproviderAndbill(id));
//        request.getSession().setAttribute("proList", pro.providerList());
        return "redirect:/jsp/"+opr+".jsp";
    }

    @RequestMapping("/updProvider")
    public String updProvider(provider pro){
        int i =prodiver.updproviderByid(pro);
        if (i>0){
            System.out.println("修改成功！！！");
        }else {
            return "jsp/providermodify";
        }

        return "redirect:getAll";
    }

    @RequestMapping("/addProvider")
    public String addProvider(provider pro,HttpServletRequest request){

        pro.setCreationDate(new Date());
        user u = (user) request.getSession().getAttribute("user");
        pro.setCreatedBy(u.getId());
        int i =prodiver.addprovider(pro);
        if (i>0){
            System.out.println("添加成功！！！");
        }else {
            return "jsp/provideradd";
        }

        return "redirect:getAll";
    }
    @RequestMapping("/delprovider")
    @ResponseBody
    public Object delBill(Integer id){

        int num = prodiver.delprovider(id);
        if (num>0){
            return  "true";
        }else {
            return  "false";
        }
    }
}



